<template>
  <div id="adPosition">
    <h1 class="con-right-title">
      广告位
    </h1>
    <!-- 上部搜索条件 -->
    <topSearch :form-inline="formInline" @toSearch="toSearch"></topSearch>
    <div class="table-conBtn">
      <el-button
        v-permission="'wh:system:adPosition:add'"
        type="warning"
        icon="el-icon-plus"
        @click="handelAddEdit('add')"
      >
        新增
      </el-button>
      <el-button
        v-permission="'wh:system:adPosition:adList'"
        type="warning"
        plain
        @click="showAdList"
      >
        广告位列表
      </el-button>
      <freshCom @searchList="searchList"></freshCom>
    </div>
    <!-- 表格部分 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      stripe
      style="width: 100%;"
      @selection-change="handleSelectionChange"
      max-height="490"
      ref="table"
    >
      <el-table-column type="index" width="80" align="center" label="序号">
        <template slot-scope="scope">
          {{
            scope.$index
              | filterPage(searchParams.pageNum, searchParams.pageSize)
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="终端"
        prop="terminalType"
        width="60"
      ></el-table-column>
      <!-- <el-table-column
        label="频道"
        prop="channel"
        min-width="100"
      ></el-table-column>-->
      <el-table-column label="位置" prop="locationType" width="80">
        <template slot-scope="scope">
          <span>
            {{ scope.row.terminalType }}-{{ scope.row.locationType }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="名称"
        prop="name"
        min-width="100"
      ></el-table-column>
      <el-table-column
        label="描述"
        prop="description"
        min-width="110"
      ></el-table-column>
      <el-table-column label="图片" width="80" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.imgInfo && scope.row.imgInfo.length > 0">
            <listImgString
              :imgInfo="scope.row.imgInfo"
              :showNum="false"
            ></listImgString>
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="图片地址" prop="imgInfo" min-width="110">
        <template slot-scope="{ row }">
          <el-tooltip class="item" effect="dark" placement="top">
            <div style="max-width: 400px;" slot="content">
              {{ row.imgInfo }}
            </div>
            <div class="oneLine">
              {{ row.imgInfo }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="跳转链接" prop="url" min-width="110">
        <template slot-scope="{ row }">
          <el-tooltip class="item" effect="dark" placement="top">
            <div style="max-width: 400px;" slot="content">
              {{ row.url }}
            </div>
            <div class="oneLine">
              {{ row.url }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" width="90">
        <template slot-scope="scope">
          <span
            v-if="scope.row.status"
            class="tag-span"
            :class="scope.row.status | filterClass('adPositionStatus')"
            >{{ scope.row.status | filterName('adPositionStatus') }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        prop="createdTime"
        width="150"
      ></el-table-column>
      <el-table-column
        label="最后修改时间"
        prop="lastModifiedTime"
        width="150"
      ></el-table-column>
      <el-table-column label="操作人" prop="lastUser.userName" min-width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.lastUser && scope.row.lastUser.userName">
            {{ scope.row.lastUser.userName }}</span
          >
          <span v-else>{{
            scope.row.createdUser && scope.row.createdUser.userName
          }}</span>
        </template></el-table-column
      >
      <el-table-column label="操作" fixed="right" width="140">
        <template slot-scope="scope">
          <el-button
            v-permission="'wh:system:adPosition:enable'"
            v-if="scope.row.status === 'TEMPORARY'"
            type="text"
            @click="handelEnabled(scope.row.id)"
          >
            启用
          </el-button>
          <el-button
            v-permission="'wh:system:adPosition:edit'"
            v-if="scope.row.status === 'TEMPORARY'"
            type="text"
            @click="handelAddEdit('edit', scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            v-permission="'wh:system:adPosition:disabled'"
            v-if="
              scope.row.status === 'OPEN' || scope.row.status === 'TEMPORARY'
            "
            type="text"
            @click="handelClose(scope.row.id)"
          >
            关闭
          </el-button>
          <el-button
            v-if="scope.row.status === 'OPEN' && scope.row.terminalType === 'H5'"
            type="text"
            @click="handelPush(scope.row.id)"
          >
            推送
          </el-button>
          <el-button
            type="text"
            v-permission="'wh:system:adPosition:copy'"
            v-if="scope.row.status === 'CLOSE'"
            @click="handelAddEdit('copy', scope.row.id)"
          >
            一键复制
          </el-button>
          <el-button
            v-permission="'wh:system:adPosition:remove'"
            v-if="scope.row.status === 'CLOSE'"
            type="text"
            @click="delItem(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-全局组件-->
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
    <!--添加弹窗 -->
    <addEditDialog
      v-if="dialogData.isShow"
      :dialog-data="dialogData"
      @refresh="refresh"
    ></addEditDialog>
    <!--广告位列表弹窗 -->
    <adListDialog v-if="AdDailog.isShow" :dialog-data="AdDailog"></adListDialog>
  </div>
</template>

<script>
import {
  getList,
  del,
  close,
  open,
  push
} from '@/api/spaceConfiguration/advertisingPosition' // 接口
import listImgString from '@common/listImgString' // 视图预览(返回值为字符串结构)-公共组件
import topSearch from '@component/spaceConfiguration/advertisingPosition/topSearch' // 上部搜索条件
import { contentToStr } from '@utils/pubFunction'
import addEditDialog from '@component/spaceConfiguration/advertisingPosition/addEditDialog' // 添加弹窗
import adListDialog from '@component/spaceConfiguration/advertisingPosition/adListDialog' // 广告位弹窗
import { saveScrollMixins } from '@/mixins/saveScroll'

export default {
  name: 'configuration',
  msg: '',
  mixins: [saveScrollMixins],
  components: {
    topSearch,
    addEditDialog,
    listImgString,
    adListDialog
  },
  data() {
    return {
      loading: false,
      total: 0, // 搜索条件
      formInline: {
        terminalType: '', //显示终端
        locationType: '', //位置
        name: '', // 名称
        sort: '', //排序
        status: '' // 状态
      },
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      // 表格数据
      tableData: [],
      dialogData: {
        isShow: false, // 默认隐藏
        id: '', // 默认新增
        type: 'add' // 默认新增
      },
      AdDailog: {
        isShow: false // 默认隐藏
      },
      multipleSelection: [],
      delMes: '',
      powerStr: 'system:adPosition:list', // 是否查询的标识
      powerAry: [], // 可显示的权限
      canShowPage: false // 是否显示当前页面
    }
  },
  activated() {
    this.toTableScrollHeight()
  },
  mounted() {
    // 获取权限
    // this.getPower()
    this.$nextTick(() => {
      setTimeout(() => {
        this.setTableScrollHeight()
      }, 1000)
    })
  },
  methods: {
    /* 获取权限 */
    getPower() {
      let id = this.$route.query.mId
      this.getCurPageMenu(id).then((res) => {
        this.powerAry = res
        if (!res.includes(this.powerStr)) {
          this.alertPower(this.powerStr)
          return false
        }
        this.canShowPage = true
      })
    },
    /* 获取列表 */
    searchList() {
      this.loading = true
      let obj = Object.assign({}, this.searchParams, this.formInline)
      getList(
        obj,
        (response) => {
          this.loading = false
          this.tableData = response.rows
          this.total = response.total
        },
        (response) => {
          this.loading = false
          this.message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    // table选择
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /* 过滤返回评论内容 */
    filterContent(item) {
      return contentToStr(item)
    },
    // 推送
    handelPush(id){
      push(
        id,
        res=>{
             this.$message.success('推送成功')
        },
        err=>{
          this.$message({
            message: err.message,
            type: 'warning'
          })
        }
        )
    },
    // 添加广告
    handelAddEdit(type, params = '') {
      this.dialogData.isShow = true
      this.dialogData.type = type
      this.dialogData.id = params
    },
    /*关闭*/
    handelClose(id) {
      this.$confirm('确定删除此位置广告吗？', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          close(
            id,
            (response) => {
              this.searchList()
            },
            (response) => {
              this.$message({
                message: response.message,
                type: 'warning'
              })
            }
          )
        })
        .catch(() => {})
    },
    // 启用
    handelEnabled(id) {
      open(
        id,
        (response) => {
          this.searchList()
        },
        (response) => {
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    /*删除*/
    delItem(id) {
      this.$confirm('确定删除该图片吗？', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          del(
            id,
            (response) => {
              this.searchList()
            },
            (response) => {
              this.$message({
                message: response.message,
                type: 'warning'
              })
            }
          )
        })
        .catch(() => {})
    },
    /*广告位列表*/
    showAdList() {
      this.AdDailog.isShow = true
    },
    /* 搜索 */
    toSearch() {
      this.searchParams.pageNum = 1
      this.searchList()
    },
    /* 刷新列表 */
    refresh() {
      this.toSearch()
    }
  }
}
</script>

<style lang="less">
#adPosition {
  @import url('~@common/style/imgBox.less');
  .oneLine {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}
</style>
